/*
 * Copyright (C) 2012 haru Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.harusource.di.delegate.impl;

import java.lang.reflect.Method;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.OnHierarchyChangeListener;

import com.harusource.di.delegate.Delegate;

/**
 * This class implements ViewGroup.OnHierarchyChangeListener.</br>
 * It handles following events of ViewGroup.</br>
 * </br>
 * * NOTE : onChildViewAdded and onChildViewRemoved are merged by the framework, these two event are 
 * dispatched to onChildViewChanged(View parent, View child, EventType type) of target object.</br>
 * <code>
 * void onChildViewAdded(View parent, View child)
 * void onChildViewRemoved(View parent, View child)
 * </code>
 * </br>
 * </br>
 * @see android.view.ViewGroup.OnHierarchyChangeListener#onChildViewAdded(View, View)
 * @see android.view.ViewGroup.OnHierarchyChangeListener#onChildViewRemoved(View, View)
 * 
 * @author Hiroki Oizumi
 */
public class ViewGroupOnHierarchyChangeListener extends Delegate implements OnHierarchyChangeListener {
    public enum EventType{
        CHILD_ADDED,
        CHILD_REMOVED
    };
    
    @Override
    public void add(Method template, Object target, Method eventhandle, View view) {
        super.add(template, target, eventhandle, view);
        ((ViewGroup)view).setOnHierarchyChangeListener(this);
    }

    @EventHandler
    public void onChildViewChanged(View parent, View child, EventType type){
        // This is a just definition of event handling for applications.
    }
    
    public void onChildViewAdded(View parent, View child) {
         Delegate.invoke3(mTarget, mEventHandles, EVENTHANDLE_DEFAULT, parent, child, EventType.CHILD_ADDED);   
    }

    public void onChildViewRemoved(View parent, View child) {
        Delegate.invoke3(mTarget, mEventHandles, EVENTHANDLE_DEFAULT, parent, child, EventType.CHILD_REMOVED);
    }
}

